package com.pv.bfc.dao;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

import com.pv.bfc.constants.BriefcaseConstants;
import com.pv.bfc.model.BriefcaseUser;
import com.pv.bfc.model.Role;
import com.pv.bfc.model.UserProfile;
import com.pv.bfc.model.UserSettings;

@Transactional
@RemoteProxy(name = "UserRepo")
public class UserJpaRepository extends AbstractFolderDAO implements
		UserRepository {

	private SessionFactory sessionFactory;

	private static final Log logger = LogFactory
			.getLog(UserJpaRepository.class);

	@Transactional(readOnly = true)
	public void deleteUser(BriefcaseUser user) {
		getJpaTemplate().remove(user);

	}

	@Transactional
	public void persistUser(BriefcaseUser user) {
		getJpaTemplate().persist(user);

	}

	@Transactional
	public void addRoles(Role role) {
		getJpaTemplate().persist(role);

	}

	@RemoteMethod
	@Transactional(readOnly = true)
	public BriefcaseUser retreiveUserByUserName(String userName) {
		return getJpaTemplate().find(BriefcaseUser.class, userName);
	}

	@RemoteMethod
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public List<BriefcaseUser> retreiveUserByEmailId(String email) {

		String query = BriefcaseConstants.USER_BY_EMAILID_SQL + "'" + email
				+ "'";

		logger.info(query);
		return getJpaTemplate().find(query);
	}

	@SuppressWarnings("unchecked")
	public List<Role> getUserRoleByUserName(String userName) {
		return getJpaTemplate().find(
				BriefcaseConstants.ROLES_BY_USERNAME_SQL + userName);
	}

	@SuppressWarnings( { "unchecked" })
	public UserSettings getUserProfile(String userName) {

		UserSettings usr_settings = new UserSettings();
		BriefcaseUser usr_dtls = null;
		UserProfile usr_prof = null;

		String query_usr_dtls = BriefcaseConstants.USER_BY_NAME_SQL + "'"
				+ userName + "'";

		String query_usr_settings = BriefcaseConstants.USER_PROFILE + "'"
				+ userName + "'";

		List usr_dtls_lst = getJpaTemplate().find(query_usr_dtls);
		List usr_prof_lst = getJpaTemplate().find(query_usr_settings);

		Iterator<BriefcaseUser> usr_itr = usr_dtls_lst.iterator();
		Iterator<UserProfile> usr_prof_itr = usr_prof_lst.iterator();

		while (usr_itr.hasNext()) {
			usr_dtls = (BriefcaseUser) usr_itr.next();
		}

		while (usr_prof_itr.hasNext()) {
			usr_prof = (UserProfile) usr_prof_itr.next();
		}

		if (usr_dtls != null || usr_prof != null) {
			usr_settings.setUser(usr_dtls);
			usr_settings.setProfile(usr_prof);
		}

		return usr_settings;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public SessionFactory getSessionFactory() {
		if (sessionFactory == null) {
			throw new IllegalStateException(
					"sessionFactory has not been set on FolderDAO before usage");
		}
		return sessionFactory;
	}

	public Session getSession() {
		return getSessionFactory().getCurrentSession();
	}

}
